#include "head.h"

ret_val_t ls_m(void)
{
    ret_val_t ret;
    ret.check_value = 0;
    memset(ret.path_info, 0, sizeof(ret.path_info));

    char* path = "."; // 当前工作目录
    DIR* dir = opendir(path);
    if (!dir)
    {
        fprintf(stderr, "open directory %s error\n", path);
        ret.check_value = -1;
        return ret;
    }

    while (1)
    {
        struct dirent* object = readdir(dir);
        if (!object)
            break;

        char path_x[1024];
        snprintf(path_x, sizeof(path_x), "%s/%s", path, object->d_name);

        struct stat filestat;
        if (lstat(path_x, &filestat) < 0)
        {
            fprintf(stderr, "stat file %s error\n", path_x);
            continue;
        }
        strcat(ret.path_info, object->d_name);
        strcat(ret.path_info, "\n");
    }

    closedir(dir);
    ret.check_value = 1;
    return ret;
}
